package com.lzy.mall.product.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.lzy.mall.common.utils.PageUtils;
import com.lzy.mall.product.dao.PmsAttrDao;
import com.lzy.mall.product.entity.PmsAttrAttrgroupRelation;
import com.lzy.mall.product.dao.PmsAttrAttrgroupRelationDao;
import com.lzy.mall.product.service.PmsAttrAttrgroupRelationService;
import com.lzy.mall.common.utils.PageParaUtils;
import com.lzy.mall.product.vo.AttrGroupRelationVo;
import org.beetl.sql.core.engine.PageQuery;
import org.springframework.stereotype.Service;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * 属性&属性分组关联(PmsAttrAttrgroupRelation)表服务实现类
 *
 * @author lzy
 * @since 2021-08-25 11:41:07
 */
@Service
@RequiredArgsConstructor(onConstructor_ = {@Autowired})
public class PmsAttrAttrgroupRelationServiceImpl implements PmsAttrAttrgroupRelationService {
    private final PmsAttrAttrgroupRelationDao relationDao;

    private final PmsAttrDao attrDao;

    @Override
    public void saveBatch(List<AttrGroupRelationVo> vos) {
        List<PmsAttrAttrgroupRelation> collect = vos.stream().map(vo -> {
            PmsAttrAttrgroupRelation relation = new PmsAttrAttrgroupRelation();
            BeanUtil.copyProperties(vo, relation);
            relation.setAttrSort(0);
            return relation;
        }).collect(Collectors.toList());
        relationDao.insertBatch(collect);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        PageQuery pageQuery = PageParaUtils.pagePara(params);
        PageQuery page = relationDao.queryPage(pageQuery);
        return new PageUtils(page);
    }

    @Override
    public PmsAttrAttrgroupRelation getById(Long id) {
        return relationDao.single(id);
    }

    @Override
    public void updateById(PmsAttrAttrgroupRelation attrAttrgroupRelation) {
        relationDao.updateTemplateById(attrAttrgroupRelation);
    }

    @Override
    public void removeByIds(ArrayList<Long> ids) {
        relationDao.createLambdaQuery()
                .andIn(PmsAttrAttrgroupRelation::getId,ids)
                .delete();
    }
}
